import {
	BASE_URL
} from './request.js';
const url = `${BASE_URL}/api/system/v1/file/upload`

export const uploadFiles = (options) => {
	console.log('???',url)
	uni.showLoading({
		title: '上传中'
	});
	var token = uni.getStorageSync("token") || "";
	return new Promise((resolve, reject) => {
		const uploadTask = uni.uploadFile({
			url,
			header: {
				'Content-Type': 'multipart/form-data;',
				token
			},
			filePath: options.filePath,
			formData: options.formData,
			name: 'file',
			success(res) {
				let data = JSON.parse(res.data)
				if (data.code == '20000' || data.code == '20009') {
					uni.removeStorageSync('userInfo');
					uni.removeStorageSync('token');
					setTimeout(() => {
						uni.showToast({
							title: '未登录，或登录失效',
							icon: "none"
						})
					}, 1000)
					uni.reLaunch({
						url: "/pages/login/login"
					})
				} else {
					uni.showToast({
						title: '上传成功！'
					})
					resolve(data)
				}

			},
			fail(err) {
				uni.showToast({
					title: '请求超时！',
					icon: 'none'
				})
				reject(err)
			},
			complete() {
				uni.hideLoading()
			}
		})
	})
}


module.exports = {
	uploadFiles
}
